package com.ryskoo.catering.orderaddress.service;

import com.ryskoo.catering.orderaddress.domain.OrderAddressPO;
import com.ryskoo.catering.orderaddress.repository.IOrderAddressRepository;
import com.ryskoo.customer.customeraddress.domain.CustomerAddressPO;
import com.ryskoo.customer.customeraddress.service.ICustomerAddressService;
import com.ryskoo.framework.core.exception.InvalidParamException;
import com.ryskoo.framework.core.service.BasicServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

/**
 * @author BeHappy
 */
@Service
public class OrderAddressServiceImpl extends BasicServiceImpl<OrderAddressPO> implements IOrderAddressService {
    @Autowired
    private IOrderAddressRepository orderAddressRepository;

    @PostConstruct
    public void init() {
        this.setDao(orderAddressRepository);
    }
    @Autowired
    private ICustomerAddressService customerAddressService;

    @Override
    public OrderAddressPO getAddressByOrderUuid(String orderUuid) {
        return orderAddressRepository.getAddressByOrderUuid(orderUuid);
    }

    @Override
    public void createAddress(String orderUuid, String addressUuid) {
        if(StringUtils.isEmpty(addressUuid)){
            throw new InvalidParamException("会员收货地址uuid为空");
        }
        CustomerAddressPO customerAddress = customerAddressService.getByUuid(addressUuid);
        if(customerAddress == null){
            throw new InvalidParamException("会员收货地址为空");
        }

        OrderAddressPO orderAddress = new OrderAddressPO();
        orderAddress.setOrderUuid(orderUuid);
        orderAddress.setName(customerAddress.getConsignee());
        orderAddress.setMobile(customerAddress.getMobile());
        orderAddress.setProvinceUuid(customerAddress.getProvince());
        orderAddress.setCityUuid(customerAddress.getCity());
        orderAddress.setRegionUuid(customerAddress.getRegion());
        orderAddress.setStreetUuid(customerAddress.getStreet());
        orderAddress.setAddress(customerAddress.getAddress());
        this.create(orderAddress);
    }
}
